<?php
namespace jy_levelfee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'代理商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('mid'=>$mid));
			}
			if(!$member['is_agent']){
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->setting['become']==0){
					//分销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->setting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待','error','jy_levelfee/apply/');
					}
				}elseif($this->setting['become']==2){
					//消费次数
					$buynums=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
					if($buynums>=$this->setting['num']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待','error','jy_levelfee/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_levelfee/apply/');
					}
				}elseif($this->setting['become']==3){
					//消费金额
					$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
					if($buyfee>=$this->setting['money']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->setting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待','error','jy_levelfee/apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_levelfee/apply/');
					}
				}elseif($this->setting['become']==4){
					//购买指定商品
					$become_goods=$this->setting['become_goods'];
					foreach($become_goods as &$bgv){
						$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
						if($isbuy>0){
							$ispass=true;
							$this->apply_agent($member['mid']);
							break;
						}
					}
					if($this->setting['become_check'] && $isexi && $ispass){
						exi('申请正在审核请耐心等待','error','jy_levelfee/apply/');
					}elseif($isexi && !$ispass){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_levelfee/apply/');
					}
				}else{
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_levelfee/apply/');
					}
				}
				if($ispass){
					exi('','','jy_levelfee/index/');
				}
			}elseif($member['stop'] && $isexi){
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_levelfee/apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($this->setting['become']['become_reg']==1){
					exi('请先完善信息','error','jy_commission/info/');
				}else{
					
				}
			}
			return $member;
	}
	public function check_myshop($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=model('core/member')->basic($mid);
		$getmember=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($member['is_agent']){
			$myshop=$this->get('shop',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if(!$myshop['id']){
				$data=array(
					'logo'=>$member['avatar'],
					'title'=>$member['title']?:$member['nickname'],
					'mid'=>$member['mid'],
					'uniacid'=>$_SESSION['uniacid'],
				);
				pdo_insert('jy_levelfee_shop',$data);
				return true;
			}
		}
		return false;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_levelfee_member',$update,array('mid'=>$mid));
		return true;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('分销商会员注册失败，缺少会员ID.');
		}
		$ismid=$this->get('member',array('mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		$this->insert('member',$member);
		if($member['agent_id']){
			$this->set_parent($member['mid'],$member['agent_id']);
		}
		if(!$member['agent_id'] && $_SESSION['this']=='custom' && $this->setting['become_child']==1){
			$agent_id=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'fromuser');
			if($agent_id){
				$this->set_parent($member['mid'],$agent_id);
			}
		}
		//如果成为分销商无条件，此处自动申请成为分销商
		if($this->setting['become']==0){
			//分销商无需审核，此处自动审核
			$this->apply_agent($member['mid']);
		}
	}
	/*
		设置上下级关系
	*/
	public function set_parent($mid,$parent_id,$ischange=false){
		$old_agent_id=$this->getcolumn('member',array('mid'=>$mid),'agent_id');
		$isuser=pdo_getcolumn('core_members',array('id'=>$parent_id),'id');
		//如果不存在该会员，或已经有上级了，取消执行。
		if(($old_agent_id || !$isuser) && !$ischange){
			return true;
		}
		//如果他的上级是我
		$ta_agent_id=$this->getcolumn('member',array('mid'=>$parent_id),'agent_id');
		if($ta_agent_id==$mid){
			return true;
		}
		//如果上级是自己，上级ID归零
		if($mid==$parent_id){
			$parent_id=0;
		}
		$this->update('member',array('agent_id'=>$parent_id),array('mid'=>$mid));
		if($this->setting['level']>0){
			$agent_id=$old_agent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
			$agent_id=$parent_id;
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				//只有一级才增加一级下级
				if($i==0){
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
						'member_num_next +='=>1,
						'agent_num_next +='=>1,
					);
				}else{
					$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
					);
				}
				$this->update('member',$member_update,array('mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				model('jy_weishop/notice')->notice_fenxiao_xiajiadd($mid,$agent_id);
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
		}
		return true;
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid){
		$apply=$this->get('apply',array('mid'=>$mid));
		if($apply && $apply['status']!=2){
			if($apply['status']==1){
				$member_update=array(
					'is_agent'=>1,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('mid'=>$apply['mid']));
			}
			return;
		}
		if($apply['status']!=2){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
			//开小店：
			$myshop=pdo_get('jy_levelfee_shop',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if(!$myshop['mid']){
				$member=pdo_get('core_members',array('id'=>$mid),array('id','avatar','nickname'));
				$shop=array(
					'logo'=>$member['avatar'],
					'title'=>$member['nickname'].'的小店',
					'mid'=>$mid,
					'uniacid'=>$_SESSION['uniacid'],
				);
				pdo_insert('jy_levelfee_shop',$shop);
			}
		}else{
			$this->update('apply',array('status'=>0),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		if(!$this->setting['become_check']){
			//分销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核分销商
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'is_agent'=>1,
				'agenttime'=>TIMESTAMP,
			);
			$this->update('member',$member_update,array('mid'=>$apply['mid']));
			//审核通过，此处增加上线的相关数据
			$agent_id=$this->getcolumn('member',array('mid'=>$apply['mid']),'agent_id');
			for($i=0;$i<$this->setting['level'];$i++){
				if(!$agent_id){
					break;
				}
				$agent_update=array('agent_num +='=>1);
				if($i==0){
					$agent_update['agent_num_next +=']=1;
				}
				$this->update('member',$agent_update,array('mid'=>$agent_id));
				$this->update_agent_level($agent_id);
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		禁用分销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复分销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
	/*
		获取会员的各项参数
		
		问题：开启分销内购时
			推广订单如何算？
			下线如何算？
			
			计算下线时，分销级数在设置的级数基础上-1，其他不变
			计算推广订单时，在上面的基础上+自己的订单
	*/
	public function get_member($mid){
		$res=array();
		//获取一级下线数量
		$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$mid),array('mid','is_agent'),'mid');		
		if(!$members){
			$res=array(
				'member_num'=>'0',
				'agent_num'=>'0',
				'order_price'=>'0',
				'order_num'=>'0',
				'myorder_price'=>'0',
				'order_price_next'=>'0',
				'order_num_next'=>'0',
				'member_num_next'=>'0',
				'agent_num_next'=>'0',
			);
			return $res;
		}
		//获取直属下级分销商数量
		$res['agent_num_next']=$this->count('member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'agent_id'=>$mid));
		$res['member_num_next']=count($members);
		$member_ids=array_keys($members);
		$res['order_num_next']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price_next']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		//获取下线总数，向下遍历系统设置的指定分销层数
		$commission_level=intval($this->setting['level']);
		if($this->setting['self_commission']){
			//开启分销内购
			$commission_level=$commission_level-1;
		}
		if($commission_level>1){
			$next_ids=$member_ids;
			for($i=1;$i<$commission_level;$i++){
				$next_members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$next_ids),array('mid','is_agent'),'mid');
				if(!$next_members){
					break;
				}
				$members=array_merge($next_members,$members);
				$next_ids=array_keys($next_members);
				$member_ids=array_merge($next_ids,$member_ids);
			}
		}
		$res['member_num']=count($member_ids);
		$res['order_num']=$this->count('order',array('mid'=>$member_ids));
		$res['order_price']=$this->sum('order',array('mid'=>$member_ids),'order_price');
		$res['myorder_price']=$this->sum('order',array('mid'=>$mid),'myorder_price');
		$res['agent_num']=0;
		foreach($members as $member){
			if($member['is_agent']){
				$res['agent_num']++;
			}
		}
		if($this->setting['self_commission']){
			//开启分销内购，此处加上用户自己的订单作为推广订单的一部分
			$self_order_num=$this->count('order',array('mid'=>$mid));
			$self_order_money=$this->sum('order',array('mid'=>$mid),'order_price');
			$res['order_num_next']=$self_order_num;
			$res['order_price_next']=$self_order_money;
			$res['order_num']+=$self_order_num;
			$res['order_price']+=$self_order_money;
		}
		return $res;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid){
		//
		$member=$this->get('member',array('mid'=>$mid));
		if(!$member['is_agent'] || $member['stop']){
			return;
		}
		$level=$this->get('level',array('id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);
		$levels=$this->get_list('level',$parameter);
		$next_level=$levels[0];
		if(!$next_level){
			return;
		}
		$conditions=json_decode($next_level['conditions'],true);
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		foreach($level_up_types as $type){
			if($member[$type]<$conditions[$type]){
				$up=false;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('mid'=>$mid));
		}
		return true;
	}
}